package com.struts.action.news;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;
import com.common.Constant;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hibernate.pojo.Articles;
import com.struts.form.ArticlesForm;
import com.spring.service.news.ArticlesService;

/**
 * @author mengqing
 *
 */
public class ArticlesAction extends DispatchAction
{
	private ArticlesService articlesService=null;
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newsAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 

	{
		ArticlesForm articlesform = (ArticlesForm) form;
		if (articlesService.newsAdd(articlesform)) {
			String path = mapping.findForward("success").getPath();
			ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
			forward.setRedirect(true);
			return forward;
		} else {
			return mapping.findForward("fail");
		}
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward findNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		int page=0;
		int articlesleibieID = 0;
		if(request.getParameter("page")!=null)
		{
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("articlesleibieID")!=null && request.getParameter("articlesleibieID")!="")
		{
			articlesleibieID=Integer.parseInt(request.getParameter("articlesleibieID"));
		}
		Map hashmap=articlesService.findNews(page,articlesleibieID);
		request.setAttribute("page_content", hashmap.get("page_content"));
		request.setAttribute("news_list", hashmap.get("news_list"));
		request.setAttribute("articlesLiebei", articlesService.findNewsTypes("from ArticlesLeibie"));
		return mapping.findForward("news_list");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		request.setAttribute("news_type", articlesService.findNewsTypes("from ArticlesLeibie where fatherId=0"));
		return mapping.findForward("news_add_view");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		Map map=articlesService.updateNewsView(id);
		request.setAttribute("update_news_view", map.get("update_news"));
		
		Articles articles=(Articles)map.get("update_news");
		request.setAttribute("content", articles.getContent());
		
		request.setAttribute("news_type", articlesService.findNewsTypes("from ArticlesLeibie"));
		//request.setAttribute("leibie_type", articlesService.findNewsTypes("from Leibie"));
		
		request.setAttribute("page", request.getParameter("page"));
		return mapping.findForward("news_update_view");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward deleteNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		if(articlesService.deleteNews(id))
		{
			 String path = mapping.findForward("success").getPath();
			 ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
			 forward.setRedirect(true);
			 return forward;
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateNewsAudit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		Long id=Long.parseLong(request.getParameter("id"));
		int audit=Integer.parseInt(request.getParameter("audit"));
		if(audit==0)
			audit=1;
		else
			audit=0;
		ArticlesForm articlesform=new ArticlesForm();
		articlesform.setId(id);
		articlesform.setAudit(audit);
		if(articlesService.updateNewsAudit(articlesform))
		{
			 String path = mapping.findForward("success").getPath();
			 ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
			 forward.setRedirect(true);
			 return forward;
		}
		else
			return mapping.findForward("fail");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ArticlesForm articlesform = (ArticlesForm) form;
		if (articlesService.updateNews(articlesform))
			{
				 String path = mapping.findForward("success").getPath();
				 ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
				 forward.setRedirect(true);
				 return forward;
			}
		 else {
			return mapping.findForward("fail");
		}
	}

	/**
	 * batch delete articles
	 * @return
	 */
	public ActionForward deleteBatchNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		String ids=request.getParameter("ids");
		if (articlesService.deleteBatchNews(ids))
			{
				String path = mapping.findForward("success").getPath();
				ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
				forward.setRedirect(true);
				return forward;
			}
		else {
			return mapping.findForward("fail");
		}
	}
	
	/**
	 * batch update articles category
	 * @return
	 */
	public ActionForward updateCategryBatchNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		System.out.println(request.getParameter("leibie"));
		Long categry=Long.parseLong(request.getParameter("leibie"));
		System.out.println(categry);
		String ids=request.getParameter("ids");
		if (articlesService.updateBatchNewsLeibie(ids,categry))
			{
				String path = mapping.findForward("success").getPath();
				ActionForward forward= new ActionForward(path+"&page="+request.getParameter("page")+"&articlesleibieID="+request.getParameter("articlesleibieID"));
				forward.setRedirect(true);
				return forward;
			} else {
			return mapping.findForward("fail");
		}
	}
	
	
	public ArticlesService getArticlesService() {
		return articlesService;
	}


	public void setArticlesService(ArticlesService articlesService) {
		this.articlesService = articlesService;
	}
}
